var xmlHttpObject = false;
var articles = new Array();
var jsDir = "";

if (typeof XMLHttpRequest != 'undefined') {
	xmlHttpObject = new XMLHttpRequest();
}
if (!xmlHttpObject) {
	try {
		xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlHttpObject = null;
		}
	}
}

// display scroll up arrow
var jQ = jQuery.noConflict();
jQ(document).ready(function() {
	jQ(window).scroll(function() {
		var height = jQ(window).scrollTop();
		if (height > 250) {
			jQ("#return_to_top").fadeIn("slow");
		} else {
			jQ('#return_to_top').fadeOut('slow');
		}
	});
});

function fetchData() {

	var torrentfeed = document.getElementById('torrentfeed').value;

	// show the loading image
	var feedElem = document.getElementById('feed');
	var titleElem = document.getElementById('name');
	var loadImage = "url('" + jsDir + "app/images/loading.gif')";

	if (feedElem.value == "") {
		feedElem.style.backgroundImage = loadImage;
	}

	if (titleElem.value == "") {
		titleElem.style.backgroundImage = loadImage;
	}

	xmlHttpObject.open('get', jsDir + 'inc/dataFetcher.php?torrentfeed='
			+ torrentfeed);
	xmlHttpObject.onreadystatechange = editInput;
	xmlHttpObject.send(null);
}

function editInput() {
	if (xmlHttpObject.readyState == 4) {
		var matches = xmlHttpObject.responseText.match(/{feedUrl:(.*?):}/);
		var feedUrl = matches[1];

		matches = xmlHttpObject.responseText.match(/{title:(.*?):}/);
		var title = matches[1];

		var feedElem = document.getElementById('feed');
		var titleElem = document.getElementById('name');

		if (feedElem.value == "") {
			feedElem.value = feedUrl;
		}

		if (titleElem.value == "") {
			titleElem.value = title;
		}

		// remove loading image
		feedElem.style.backgroundImage = "";
		titleElem.style.backgroundImage = "";
	}

}

function replaceAudioPlayer(playerId) {
	var audioTag = document.createElement(playerId);
	var src = document.getElementById(playerId).lang;

	jwplayer(playerId).setup( {
		'flashplayer' : jsDir + "app/mediaplayer/player.swf",
		'file' : src,
		'controlbar' : 'bottom',
		'width' : '100%',
		'height' : '24',
		'autostart' : 'true'
	});

	showUnloadWarning();
}

function deletePodcast(tag) {
	if (confirm('Are you sure you want to delete this podcast from the list?')) {
		document.getElementById('tag').value = tag;
		document.forms["delPodcast"].submit();
	}
}

function scrollToElement(id, error) {
	try {

		var testElement = document.getElementById(id);
		var normal;
		var highlight;
		var duration;
		var offset;

		if (error != "") {
			document.getElementById('addFeedMsg').innerHTML = error;
			document.getElementById('addFeedMsg').style.color = "#ffffff";

			normal = '4A4A4A';
			highlight = 'ff0000';
			duration = 10;
			offset = 0;
		} else {
			normal = 'E9E9E9';
			highlight = 'FFF79F';
			duration = 6;
			offset = -100;
		}

		// scroll to element
		new Effect.ScrollTo(id, {
			offset : offset
		});

		// highlight element
		new Effect.Highlight(id, {
			startcolor : '#' + highlight,
			endcolor : '#' + normal,
			duration : duration
		});

	} catch (err) {
		// just do nothing
	}
}

function setRootDir(dir) {
	jsDir = dir;
}

function clearSearch() {
	try {
		var search = document.getElementById('search');
		search.value = "";

		filterArticles();
	} catch (err) {
		// do nothing
	}
}

function pushToArticles(elem, tag) {
	tmp = new Array();
	tmp[0] = elem.toLowerCase();
	tmp[1] = tag.toLowerCase();
	
	articles.push(tmp);
}

function filterArticles() {
	var search = document.getElementById('search').value;
	search = search.toLowerCase();

	var counter = 0;
	for ( var i = 0; i < articles.length; i++) {
		var myrxp = new RegExp("\\b.*" + search + ".*\\b", "gi");

		var check = (myrxp.test(articles[i][0]));
		if (check) {
			document.getElementById('article_' + i).style.display = "";
			counter++;
		} else {
			document.getElementById('article_' + i).style.display = "none";
		}
	}

	document.getElementById('nbOfFeeds').innerHTML = "There are " + counter
			+ " podcast feeds available:";
}

function goToArticle() {
	// if there is only 1 podcast left redirect to its URL
	var counter = 0;
	var tag;
	for ( var i = 0; i < articles.length; i++) {
		if (document.getElementById('article_' + i).style.display != "none") {
			tag = articles[i][1],
			counter++;
		}
	}
	
	if(counter == 1) {
		window.location.href = jsDir + 'episodes/' + tag;
	}
	
}

// show warning on page unload if neccessary
function showUnloadWarning() {
	window.onbeforeunload = function() {
		return "You're playing an episode.";
	}
}